home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / misc / biology / treedraw.sit / Tree Draw Deck / card_11064.txt < prev    next >
Encoding:
Text File  |  1990-06-26  |  14.7 KB  |  569 lines

  1. -- card: 11064 from stack: in
  2. -- bmap block id: 0
  3. -- flags: 0000
  4. -- background id: 9703
  5. -- name: 
  6.  
  7.  
  8. -- part contents for background part 3
  9. ----- text -----
  10. hp2pict.inc.c
  11.  
  12. -- part contents for background part 2
  13. ----- text -----
  14.  
  15. /* hp2pict.inc.c
  16.  *
  17.  *
  18.  * Copyright 1990 by d.g.gilbert.
  19.  * dogStar Software && Indiana University Biology Dept.
  20.  * email: gilbertd@iubio.bio.indiana.edu
  21.  *
  22.  */
  23.  
  24.  
  25.  
  26. /* hp2pict routines & data */
  27.  
  28. /* A4: 10800 x, 7680 y -- GCG driver: 10170-44x, 7840-384y */
  29. /* A3: 15200 x, 10800 y -- GCG driver: 16000-0x, 10170-34y*/
  30. /* 9600 x 7200 == PlotGram idea of hp pagesize */
  31. #define hpPageX   10800   /*10800 */     /*10170 - 44 */
  32. #define hpPageY   7680   /*7456*/ /*7680 */
  33.  
  34. #define LBEND     '~' /* LaBel end char, was 3 (^C),
  35.                       need non-control to work in HCard field...
  36.                       'ΓÇá' -- !! HI ascii is missed by this C !! */
  37.  
  38.  
  39. /*
  40.               MUST #INCLUDE <MATH.H>
  41.               or get garbage at runtime without warning
  42. */
  43. int  itan2( float x, float y)
  44. {  /* == atan2, but return angle as int degrees (0..360) */
  45. #define  mpi   3.1415926
  46.   int     r;
  47.  
  48.   if (x==0.0 && y==0.0) r = 360;
  49.   else if (x==0.0) r = 0;
  50.   else if (y==0.0) r = 90;
  51.   else r = (180.0/mpi * atan(fabs(x/y)));
  52.   if (y < 0.0) r = 180 - r;
  53.   r = abs(r);
  54.   if (x < 0.0) r = 360 - r;
  55.   return r;
  56. } /* itan2 */
  57.  
  58. void  lineit(int linetype, int dashsize)
  59. { /* ?? need turtlegraphics to do dashed lines ? */
  60.   /* not working yet */
  61.  
  62.   switch (linetype) {
  63.     case 0:
  64.       /* dashstop(); */
  65.       break;
  66.     default:
  67.     case 1:
  68.     case 2:
  69.     case 3:
  70.     case 4:
  71.     case 5:
  72.     case 6:
  73.       dashsize = dashsize;
  74.       /* dashline(dashsize); */
  75.       break;
  76.     }
  77. }
  78.  
  79.  
  80.  
  81. void  laserline( int num, int denom)
  82. /* set postscript line width (rel to 1/72 dpi);
  83.   use num=1, denom=4 for 1/300 dpi line */
  84. {
  85. #define SetLineWidth    182
  86.   Point   **linesize;
  87.  
  88.   (Handle) linesize = NewHandle(sizeof(Point));
  89.   (**linesize).v = num;
  90.   (**linesize).h = denom;
  91.   PicComment( SetLineWidth, sizeof(Point), (Handle) linesize);
  92.   DisposHandle( (Handle) linesize);
  93. } /*laserline*/
  94.  
  95.  
  96.  
  97.  
  98. void  drawUpstr(int h, int v, int rotnum, char *s)
  99. {
  100. #define maxd  24
  101. #define maxdb 4   /* must be even */
  102. #define maxw  816 /* was 512 */
  103. #define maxwb 102 /* was 64 -- must be even */
  104.  
  105. typedef char  sbitarray[maxd][maxwb];
  106. typedef char  tbitarray[maxw][maxdb];
  107.  
  108. sbitarray *sbits, *sbit2;
  109. tbitarray *tbits;
  110. Ptr       tp;
  111. int       w,d,i,j;
  112. Rect      tor, sor;
  113. FontInfo  fi;
  114. BitMap    smap, smap2, tmap;
  115. GrafPtr   aport, offport;
  116.  
  117.  
  118.   GetPort( &aport); /* !! may be CGrafPort w/ pixmap */
  119.   GetFontInfo(&fi);
  120.   d  = min(maxd-1, fi.ascent + fi.descent + fi.leading);
  121.   w  = min(maxw-1, stringwidth(s));
  122.   /* h  = h - (d / 2); */  /*let h,v == topleft not center*/
  123.   /* v  = v - (w / 2); */
  124.   SetRect( &tor, h, v, h + d, v + w);
  125.   SetRect( &sor, h, v, h + w, v + d);
  126.  
  127.   (Ptr)sbits = NewPtr(sizeof(sbitarray));
  128.   for (i=0; i<sizeof(sbitarray); i++) ((char *)sbits)[i] = 0;
  129.   smap.baseAddr = (Ptr)sbits;
  130.   if ((long)smap.baseAddr % 2 == 1) ++smap.baseAddr; /* if odd(addr) */
  131.   smap.rowBytes  = maxwb;
  132.   SetRect( &smap.bounds, 0, 0, w, d);
  133.  
  134.   switch (rotnum) {
  135.    case 2:
  136.     (Ptr)sbit2 = NewPtr(sizeof(sbitarray));
  137.     for (i=0; i<sizeof(sbitarray); i++) ((char *)sbit2)[i] = 0;
  138.     smap2.baseAddr = (Ptr)sbit2;
  139.     if ((long)smap2.baseAddr % 2 == 1) ++smap2.baseAddr;
  140.     smap2.rowBytes  = maxwb;
  141.     SetRect( &smap2.bounds, 0, 0, w, d);
  142.     break;
  143.  
  144.    case 1:
  145.    case 3:
  146.     (Ptr)tbits = NewPtr(sizeof(tbitarray));
  147.     for (i=0; i<sizeof(tbitarray); i++) ((char *)tbits)[i] = 0;
  148.     tmap.baseAddr = (Ptr)tbits;
  149.     if ((long)tmap.baseAddr % 2 == 1) ++tmap.baseAddr;
  150.     tmap.rowBytes = maxdb;
  151.     SetRect( &tmap.bounds, 0, 0, d, w);
  152.     break;
  153.     }
  154.  
  155.   (Ptr) offport = NewPtr(sizeof(GrafPort)); /* forget CGrafPort */
  156.   OpenPort( offport);
  157.   SetPortBits( &smap);      /* set our offscreen map */
  158.   TextFont( aport->txFont);
  159.   TextSize( aport->txSize);
  160.   TextFace( aport->txFace);
  161.   MoveTo(0, fi.ascent);
  162.   drawstring(s);            /* draw the bugger */
  163.   ClosePort( offport);
  164.   DisposPtr( (Ptr)offport);
  165.   SetPort( aport);
  166.  
  167.   switch (rotnum) {
  168.  
  169.     case 2: /* 180┬░  ???*/
  170.       for (i=0; i < w; i++) {
  171.         for (j = 0; j < d; j++)
  172.           if (BitTst( &((*sbits)[j][0]), i))
  173.             BitSet( &((*sbit2)[d-j][0]), w-i);
  174.           }
  175.       OffsetRect( &sor, -w, 0/*-d*/); /* ? */
  176.       CopyBits( &smap2, &(aport->portBits), &smap2.bounds, &sor,
  177.             srcOr, NULL);
  178.       DisposPtr( (Ptr)sbit2);
  179.       break;
  180.  
  181.     case 0: /* here for completeness, or just drawstring */
  182.       CopyBits( &smap, &(aport->portBits), &smap.bounds, &sor,
  183.             srcOr, NULL);
  184.       break;
  185.  
  186.  
  187.     case 3:  /* = 270┬░, bottom left is start */
  188.       for (i = w - 1; i >= 0; i--) {
  189.         tp = &((*tbits)[w - (i + 1)][0]);
  190.         for ( j = 0; j < d; j++)
  191.           if (BitTst( &((*sbits)[j][0]), i)) BitSet(tp, j);
  192.         }
  193.       OffsetRect( &tor, -d, -w); /* ! */
  194.       CopyBits( &tmap, &(aport->portBits), &tmap.bounds, &tor,
  195.             srcOr, NULL);
  196.       DisposPtr( (Ptr)tbits);
  197.       break;
  198.  
  199.     case 1:   /* = 90┬░, top right is start */
  200.       for (i = 0; i<w; i++) {
  201.         tp = &((*tbits)[i][0]);
  202.         for (j = d - 1; j >= 0; j--)
  203.           if (BitTst( &((*sbits)[j][0]), i)) BitSet(tp, d - (j + 1));
  204.         }
  205.       CopyBits( &tmap, &(aport->portBits), &tmap.bounds, &tor,
  206.             srcOr, NULL);
  207.       DisposPtr( (Ptr)tbits);
  208.       break;
  209.     }
  210.  
  211.   DisposPtr( (Ptr)sbits);
  212. } /* drawUpstr */
  213.  
  214.  
  215.  
  216. void textPicDraw(int h, int v, int rot, char *s)
  217. {
  218. /* MacDraw standard drawString w/ rotation
  219.   h,v = _center_ of text rect
  220. */
  221. #define textBegin     150
  222. #define textEnd       151
  223. #define textCenter    154
  224.  
  225.   typedef struct {
  226.     char  tjus;     /* 0..4 == left,cent,right,full just */
  227.     char  tflip;    /* 0..2 == 0,horiz,vert flip */
  228.     short trot;     /* 0..360 rotate */
  229.     char  tline;    /* 1,2,3 line spacing */
  230.     char  tcmnt;    /* reserved */
  231.     } TxtPicRec, *pTxtPicRec, **hTxtPicRec;
  232.  
  233.   typedef struct  {
  234.     Fixed y, x;
  235.     } TxtCenter, *pTxtCenter, **hTxtCenter;
  236.  
  237.   int           w, d, rotnum;
  238.   Rect          zerorect;
  239.   FontInfo      fi;
  240.   hTxtPicRec    picdat;
  241.   hTxtCenter    xydat;
  242.   RgnHandle     saveclip;
  243.  
  244.   rotnum = ((rot+44) % 360) / 90;
  245.   GetFontInfo( &fi);
  246.   d = (fi.ascent + fi.descent + fi.leading) / 2;
  247.   w = stringwidth(s) / 2;
  248.   MoveTo( h, v); /* ?? for HP text */
  249.   /* MoveTo(h - w, v - d); */ /* move from center */
  250.  
  251.   (Handle) picdat = NewHandle(sizeof(TxtPicRec));
  252.   (**picdat).tjus = 1;
  253.   (**picdat).tflip = 0;
  254.   (**picdat).trot = rot % 360;
  255.   (**picdat).tline = 1;
  256.   (**picdat).tcmnt = 0;
  257.   PicComment(textBegin, 6, (Handle) picdat);
  258.   DisposHandle( (Handle) picdat);
  259.  
  260.   (Handle)xydat = NewHandle(sizeof(TxtCenter));
  261.   (**xydat).x = Long2Fix(0);
  262.   (**xydat).y = Long2Fix(0);
  263.   PicComment( textCenter, 8, (Handle)xydat);
  264.   DisposHandle( (Handle)xydat);
  265.  
  266.   if (rotnum == 0) drawstring(s);
  267.   else {
  268.     saveclip = NewRgn();
  269.     GetClip( saveclip);
  270.     SetRect( &zerorect, 0,0,0,0);
  271.     ClipRect( &zerorect);
  272.     drawstring(s);
  273.     SetClip( saveclip);
  274.     DisposeRgn( saveclip);
  275.     drawUpstr( h, v, rotnum, s);
  276.     }
  277.   PicComment( textEnd, 0, NULL);
  278. } /*textPicDraw*/
  279.  
  280.  
  281.  
  282.  
  283.  
  284. #define map2pic(x,y,h,v) { \
  285.   h = picrect->left + (x / xscale); \
  286.   v = picrect->top + ((ymax - y) / yscale); \
  287.   }
  288. #define maprel2pic(x,y,h,v) { \
  289.   h = (x / xscale); v = (( - y) / yscale); \
  290.   }
  291.  
  292. PicHandle   hp2pict( Boolean forpage, Rect *picrect, Handle hbuf,
  293.             WindowPtr myWind, Boolean *alldone)
  294. {
  295.   char      *c, *ce, *si, s[256];
  296.   int       cmd, hpx, hpy, x, y, xd, yd, h, v, k, pageno;
  297.   Boolean   dorel, atend, done, moveit, penup;
  298.   Rect      wr, hprect;
  299.   Point     pt;
  300.   float     fx, fy, linedash, xscale, yscale;
  301.   int       linetype, ymax, rot = 0;
  302.   RGBColor  bcolor;
  303.   PicHandle pic;
  304.  
  305.   HLock( hbuf);
  306.   c = (char *) (*hbuf);
  307.   while ((*c != 0) & (*c != ';')) c++;
  308.   if (*c == ';') c++;
  309.   hpx = hpPageX;
  310.   hpy = hpPageY;
  311.  
  312. redraw:
  313.  
  314.   pageno = 0;
  315.   *alldone = false;
  316.   pic = OpenPicture(picrect);
  317.   if (forpage) {
  318.     InsetRect( picrect, 5, 5);
  319.     }
  320.   else {
  321.     InsetRect( picrect, 10, 10);
  322.     OffsetRect( picrect, 0, 5);
  323.     ShowPen();
  324.     if (hasColorQD()) {
  325.       bcolor.red  = 56000;
  326.       bcolor.green= 60000;
  327.       bcolor.blue = 65000;
  328.       if (lotsOfColor(myWind)) RGBBackColor( &bcolor);
  329.       EraseRect( &myWind->portRect);
  330.       }
  331.     }
  332.   SetRect( &wr, -10000, -10000, 30000, 30000);
  333.   ClipRect(&wr);
  334.  
  335.   TextFont(times);
  336.   if (forpage) TextSize(12);
  337.   else TextSize(9);
  338.   laserline(1, 2);
  339.   PenNormal();
  340.   penup = true;
  341.   dorel = false;
  342.   linetype  = 0;
  343.   linedash  = 1;
  344.  
  345.   SetRect( &hprect, 0, 0, hpx, hpy);
  346.   ymax = hpy;
  347.   xscale = hpx / (picrect->right - picrect->left);
  348.   yscale = hpy / (picrect->bottom - picrect->top);
  349.  
  350.   done = (*c == 0);
  351.  
  352.   while (!done) {
  353.     while ((!Isalpha(*c)) && (*c != 0)) c++; /* skip to start of cmd */
  354.     /* ^^ this messes up '\27.' end of transmission */
  355.  
  356.     cmd = (Toupper(*c++))*256 + (Toupper(*c++));
  357.     atend = false;
  358.  
  359.     switch (cmd) {
  360.  
  361.       case '\27.': /* 6958 == '\27.' */ /* not seen ? */
  362.         if (*c == ')') {
  363.           atend = true;
  364.           done = true;
  365.           *alldone = true;
  366.           *c = 0;
  367.           }
  368.         break;
  369.  
  370.       case 'AF':
  371.         /* the end-of-plot signal is "AF;\27.)x" */
  372.         /* multi-page plots will want me to throw up a new
  373.            plot window -> from Draw */
  374.         if ( (++pageno > 1) | (c[1] == '\27' & c[3] == ')') ) {
  375.           atend = true;
  376.           done = true;
  377.           *c = 0;
  378.       /* SysBeep(1); */
  379.           }
  380.         break; /* autopage */
  381.  
  382.       case 'IN':
  383.         /* the start-of-plot signal for GCG is */
  384.         /* "\27.(\27.N;19:\27.I81;;17:IN;...AF;" */
  385.         break; /* initialize */
  386.  
  387.       case 'AA': break; /* arc */
  388.       case 'IP': break; /* frame */
  389.  
  390.       case 'LB':
  391.         for (si=0; *c != LBEND & si < 256; si++, c++)
  392.             s[si] = *c;
  393.         s[si] = 0;
  394.         /* drawstring(s); */
  395.         /*  -- use last valid h,v, rot */
  396.         /* Note: this h,v is left,top? of string, not center */
  397.         textPicDraw( h, v, rot, s);
  398.         ++c; atend = true;
  399.         break; /* LBlabel^C; */
  400.  
  401.       case 'LO':
  402.         x = Strtod( c, &ce); if (ce == c) break;
  403.         c = ce;
  404.         break; /* ? Label Offset move ?? */
  405.  
  406.       case 'LT':
  407.         k = linetype;
  408.         linetype = 0;
  409.         if (*c != ';') {
  410.           linetype = Strtod( c, &ce);
  411.           if (ce != c) {
  412.             c = ce; if (*c == ',') c++;
  413.             linedash = Strtod( c, &ce);
  414.             c = ce;
  415.             }
  416.         }
  417.         if (linetype != k | linetype > 0) {
  418.           maprel2pic(linedash,linedash, xd, yd);
  419.           xd = max( abs(xd), abs(yd));
  420.           lineit( linetype, xd);
  421.           }
  422.         break; /* LT#,#; line type (style, %diag) */
  423.  
  424.       case 'PU':
  425.         penup = true;
  426.         if (*c == ';') break;
  427.         x = Strtod( c, &ce); if (ce == c) break;
  428.         c = ce; if (*c == ',') c++;
  429.         y = Strtod( c, &ce); if (ce == c) break;
  430.         c = ce;
  431.         map2pic( x, y, h, v);
  432.         MoveTo(h,v);
  433.         break; /* PU[#,#];penup */
  434.  
  435.       case 'PR':  /* PR#,#[...]; rel move */
  436.         dorel = true;
  437.         goto penat;
  438.  
  439.       case 'PD':  /* PD[#,#,...]; pendn */
  440.         penup = false;
  441.         goto penat;   /* slide into PA loop */
  442.  
  443.       case 'PA':
  444.         dorel = false;
  445.         moveit = (*c != ',');
  446. penat:
  447.         do {
  448.           if (*c == ',') c++;
  449.           x = Strtod( c, &ce); if (ce == c) break;
  450.           c = ce; if (*c == ',') c++;
  451.           y = Strtod( c, &ce); if (ce == c) break;
  452.           c = ce;
  453.           if (dorel) {
  454.             maprel2pic( x, y, h, v);
  455.             if (penup | moveit) Move(h,v);
  456.             else Line(h,v);
  457.             GetPen(&pt);
  458.             h = pt.h; v = pt.v; /* save for LB */
  459.             }
  460.           else {
  461.             map2pic( x, y, h, v);
  462.             if (penup | moveit) MoveTo(h,v);
  463.             else LineTo(h,v);
  464.             }
  465.           moveit = false;
  466.           } while (true);
  467.         break; /* PA#,#[,#,#...]; mvabs */
  468.  
  469.       case 'RO':
  470.         x = Strtod( c, &ce);
  471.         if ((x > 60 & x < 120) && !forpage) {
  472.           /* can't do for printed page?? -- need to mangle  
  473.              page orientation in pageSetup... */
  474.           hpx = hpPageY;
  475.           hpy = hpPageX;
  476.  
  477.           xd = picrect->right;
  478.           picrect->right = picrect->bottom;
  479.           picrect->bottom = xd;
  480.  
  481.           if (!forpage) HidePen();
  482.           ClosePicture();
  483.           KillPicture( pic);
  484.  
  485.           xd = myWind->portRect.right - myWind->portRect.left;
  486.           yd = myWind->portRect.bottom - myWind->portRect.top;
  487.           SizeWindow( myWind, yd, xd, true);
  488.           BringToFront( myWind);
  489.           goto redraw;
  490.           }
  491.         break; /* RO#; rotate plot */
  492.  
  493.       case 'SP':
  494.         x = Strtod( c, &ce);
  495.         switch (x) {
  496.           /* pen colors should be user option */
  497.           /* this form works w/ PICT1(b/w) as well as PICT2 */
  498.           default:
  499.           case 1: ForeColor(blackColor); break;
  500.           case 2: ForeColor(greenColor); break;
  501.           case 3: ForeColor(blueColor); break;
  502.           case 4: ForeColor(redColor); break;
  503.           case 5: ForeColor(cyanColor); break;
  504.           case 6: ForeColor(magentaColor); break;
  505.           case 7: ForeColor(yellowColor); break;
  506.           case 8: ForeColor(blackColor); break;
  507.           }
  508.         /* set color to # */
  509.         break; /* SP[#]; select pen */
  510.  
  511.       case 'SI':
  512.         if (*c == ';') break;
  513.         fx = Strtod( c, &ce); if (ce == c) break;
  514.         c = ce; if (*c == ',') c++;
  515.         fy = Strtod( c, &ce); if (ce == c) break;
  516.         c =  ce;
  517.         /* map fx,fy into TextSize ?? */
  518.         break; /* SI#,#; set text abs width,abs height ? */
  519.  
  520.       case 'DI':
  521.         rot = 0;
  522.         if (*c == ';') break;
  523.         fy = Strtod( c, &ce); if (ce == c) break;
  524.         c = ce; if (*c == ',') c++;
  525.         fx = Strtod( c, &ce); if (ce == c) break;
  526.         c =  ce;
  527.         rot = itan2( fx, fy);
  528.         break; /* DI#,#; text direction */
  529.               /* (run = cos(direction), rise = sin(direction) */
  530.  
  531.       case 'SL':
  532.         break; /* SL[#]; set text slant (tangent) ? */
  533.  
  534.       case 'SC': break; /* scale */
  535.       case 'VS': break; /* VS#; pen speed */
  536.  
  537.       case 'CI':   /* filled oval */
  538.         x = Strtod( c, &ce); if (ce == c) break;
  539.         c = ce; if (*c == ',') c++;
  540.         y = Strtod( c, &ce); if (ce == c) break;
  541.         c = ce;
  542.         maprel2pic( x+1, y+1, h, v);
  543.         h = min(h,abs(v));  /* h is bad(huge) for card-sized plot */
  544.         GetPen(&pt);
  545.         SetRect(&wr, pt.h, pt.v, pt.h, pt.v);
  546.         InsetRect(&wr, -h, -h);
  547.         /* FillOval(&wr, black); */
  548.         PaintOval(&wr);
  549.         break;
  550.  
  551.       default : break;
  552.       }
  553.  
  554.     if (!atend)
  555.       while ((*c != 0) && (*c != ';') && (!Isalpha(*c)))
  556.         c++; /* skip to end cmd */
  557.     if (*c == ';') c++;
  558.  
  559.     if (*c == 0) done = true;
  560.     }
  561.  
  562.   if (!forpage) HidePen();
  563.   ClosePicture();
  564.   HUnlock( hbuf);
  565.  
  566.   return pic;
  567.  
  568. } /* hp2pict */
  569.